/**
 * @file imageProcess.cc
 * @author your name (you@domain.com)
 * @brief 图像处理相关事务
 * @version 0.1
 * @date 2022-09-17
 * 
 * @copyright Copyright (c) 2022
 * 
 */

#include "imageProcess.h"
//定义线程间传递数据的队列数据结构和互斥锁
mutex mtxQueueReadImage_640;
queue<pair<pair<string,Mat>,Mat>> queueReadImage_640;
mutex mtxQueueReadImage_368;
queue<pair<pair<string,Mat>,Mat>> queueReadImage_368;

extern int target_368;

//图像自适应等比例缩放并填充黑边 - 功能代码
#define Min(a,b) (a<b?a:b)
static cv::Mat letterBox(cv::Mat orig_img, int height, int width)
{
    cv::Mat temp, resimg;
    int img_width = orig_img.cols;
    int img_height = orig_img.rows;

    float r = Min((float)height/img_height,(float)width/img_width);
    
    int new_unpad_height = int(img_height * r);
    int new_unpad_width = int(img_width * r);

    int dh = (height - new_unpad_height);
    int dw = (width -new_unpad_width);

    float pad_h = float(dh) / 2;
    float pad_w = float(dw) / 2;

    int top = (int)(pad_h + 0.5);
    int bottom = (int)(pad_h);
    int left = (int)(pad_w + 0.5);
    int right = (int)(pad_w);
    cv::resize(orig_img,temp,cv::Size(new_unpad_width,new_unpad_height), cv::INTER_LINEAR);
    cv::copyMakeBorder(temp, resimg, top, bottom, left, right,cv::BORDER_CONSTANT,(114,114,114));
    // cv::imwrite("makeBorder.jpg",resimg);
    return  resimg;
}

void draw_rect(string pic_name, cv::Mat img, cv::Rect r){
    cv::rectangle(img, cv::Point(cvRound(r.x), cvRound(r.y)),
                    cv::Point(cvRound((r.x + r.width)), cvRound((r.y + r.height))),
                    Scalar(255,0,0), 3, 8, 0);
    string saveImg = pic_name.substr(pic_name.rfind("/") + 1, pic_name.rfind(".") - pic_name.rfind("/") - 1);
    //保存到outw文件夹
    saveImg = "out/" + saveImg + "_out.jpg";
    cv::imwrite(saveImg, img);
}

int glob_dir_And_push_img_queue(char * dirName)
{
    vector<string> file_list;
    struct dirent **namelist;
    int n;
    n = scandir(dirName, &namelist, 0, alphasort);

    if(n < 0)
    { 
        cout << "scandir return "<< n  << endl;
        return -1;
    }
    else
    {
        int index=0;
        while(index < n)
        {
            string s = namelist[index]->d_name;
            if(s != "." && s != ".."){
                //Todo:在这里将原帧率视频处理成5FPS
                if(index % 2 == 0)  
                    file_list.push_back(dirName + s);
            }
            free(namelist[index]);
            index++;
        }
        free(namelist);
    }
    //将图片进行读取并放入队列中
    target_368 = file_list.size();
    n = target_368;
    for(int i=0; i < n; i++)
    {   
        string pic_name = file_list[i];
        cv::Mat img = cv::imread(pic_name);
        
        cv::Mat resimg_368 = letterBox(img,368,368);
        cv::Mat resimg_640 = letterBox(img,640,640);
        //cv::cvtColor(resimg, resimg, cv::COLOR_BGR2RGB);
        pair<string,Mat> img_info_pair = make_pair(pic_name,img);
        //将读入图片和rknn_input 放入pair中，然后放入队列，之前先锁住
        mtxQueueReadImage_640.lock();
        queueReadImage_640.push(make_pair(img_info_pair,resimg_640));
        mtxQueueReadImage_640.unlock();
       
        mtxQueueReadImage_368.lock();
        queueReadImage_368.push(make_pair(img_info_pair,resimg_368));
        mtxQueueReadImage_368.unlock();
    }
    return 0;
}

//根据nums指定每隔多少张检测一次
Mat select_img_from_368_queue(int nums){
    pair<pair<string,Mat>,Mat> image_pair;
    for(int i = 0; i< nums; i++)
    {
        mtxQueueReadImage_368.lock();
        if(queueReadImage_368.empty())
        {   
            mtxQueueReadImage_368.unlock();
            usleep(1000);
            i--;
        }
        else
        {
            image_pair = queueReadImage_368.front();
            queueReadImage_368.pop();
            mtxQueueReadImage_368.unlock();    
        } 
    }
    return image_pair.second;
}


Mat select_img_from_640_queue(int nums){
    pair<pair<string,Mat>,Mat> image_pair;
    for(int i = 0; i< nums; i++)
    {
        mtxQueueReadImage_640.lock();
        if(queueReadImage_640.empty())
        {   
            mtxQueueReadImage_640.unlock();
            usleep(1000);
            i--;
        }
        else
        {
            image_pair = queueReadImage_640.front();
            queueReadImage_640.pop();
            mtxQueueReadImage_640.unlock();    
        } 
    }
    return image_pair.second;
}